//
//  CaseManagerViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//

#import "CaseManagerViewController.h"
#import "HCSicknessManageTableViewCell.h"
#import "addillnessHistory.h"
#import "AddillnessHistoryViewController.h"
#import "HCPatientModel.h"
#import "ProgressManager.h"

static NSString *BLCellID=@"BLcellID";
@interface CaseManagerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,weak)addillnessHistory *bottomView;
@property(nonatomic,assign)NSInteger count;

@property(nonatomic,strong)NSMutableArray<HCPatientModel *> *dataList;

@end

@implementation CaseManagerViewController

//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    
//    NSInteger count = [MLDefaults integerForKey:@"dataList.count"];
//    
//    _count = count;
//    
//    
//    NSLog(@"%zd",count);
//}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self pushSecondView];
    
    [self setupUI];
    
    [self loadData];
    
}

- (void)loadData{
    
    if(![MLDefaults boolForKey:kIsUserLoginKey]){
        
        [ProgressManager  alertInfoStatus:@"对不起,你没有登录"];
        return;
    }
    
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:[NSString stringWithFormat:@"hc_illnessHistory.plist"]];
    
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    _dataList = [array mutableCopy];
    
    [self.tableView reloadData];
}


-(void)setupUI
{
    
    _dataList = [NSMutableArray array];
    
    UITableView *tableView = [[UITableView alloc]init];
    
    self.tableView = tableView;
    
    tableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1];
    
    [self.view addSubview:tableView];
    
    //设置代理
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    _tableView.rowHeight = 225;
    
    [_tableView registerNib:[UINib nibWithNibName:@"HCSicknessManageTableViewCell" bundle:nil] forCellReuseIdentifier:BLCellID];
    //设置约束
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(64);
        
        make.left.right.equalTo(self.view);
        
        make.bottom.equalTo(self.bottomView.mas_top);
    }];    
}

//push到第二个页面
-(void)pushSecondView
{
    UINib *nib = [UINib nibWithNibName:@"addillnessHistory" bundle:nil];
    
    addillnessHistory *view =  [[nib instantiateWithOwner:nil options:nil]lastObject];
    
    [self.view addSubview:view];
    
    _bottomView = view;
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.mas_equalTo(self.view).offset(0);
        
        make.height.mas_equalTo(60);
        
    }];
    
    __weak CaseManagerViewController *wealSelf=self;
    
    view.illnessHistoryBlock = ^{
        
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"addillnessHistoryStoryboard" bundle:nil];
        
        AddillnessHistoryViewController *vc = sb.instantiateInitialViewController;
        
        vc.index = _dataList.count;
        
        vc.callBackBlock = ^(HCPatientModel *model){
        
            [self loadData];
            
            [wealSelf.tableView reloadData];
        
        };
        
        [ wealSelf.navigationController pushViewController:vc animated:YES];

    };
    
}

#pragma mark
#pragma mark -delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataList.count;
   
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HCSicknessManageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:BLCellID forIndexPath:indexPath];
    
    //_count 是存到偏好设置里de,indexPath.row是获取当期的行数
    HCPatientModel *model = _dataList[indexPath.row];

        
        cell.model = model;
        
        return cell;
        
}

- (void)goBack
{
    [super goBack];
    
    [MLDefaults setInteger:_dataList.count forKey:@"dataList.count"];
    
}

@end
